<?php

class organization extends Model
{
	function getOrgName( $orgid ) {
		$rs = $this->db->getAll('
			SELECT name
			FROM organization
			WHERE org_id = ?',
			array( (int) $orgid )
		);
		if( count( $rs ) === 1 ) return $rs[0]['name'];
		else throw new Exception("Org does not exist");
	}	
		
	function getOrgId( $orgname ) {
		$rs = $this->db->getAll('
			SELECT org_id
			FROM organization
			WHERE name = ?',
			array( $orgname )
		);
		if( count( $rs ) === 1 ) return $rs[0]['org_id'];
		else throw new Exception("Org does not exist");
	}
		
	function getOrgByName($orgname)	
	{
		$gorg = $this->db->getAll("SELECT 
			org_id, name, description, long_description, info, css, logo
			FROM organization
			WHERE name = ?",
			array( $orgname )		
		);
		return $gorg;
	}

	function getPublicOrgs() {
		$gorg = $this->db->getAll("SELECT 
			name,	description, long_description, logo, org_id
			FROM organization
			ORDER BY description
			"
		);
		return $gorg;
	}

	function getMemberOrgs( $stud_no ) {
		return $this->db->getAll("
			SELECT *
			FROM
			organization o,
			members m,
			user u
			WHERE o.org_id = m.org_id AND m.stud_no = u.stud_no
			AND u.stud_no = ?
			",
			array( $stud_no )
		);
	}
	
	function getMemberOrgsWithAdmin( $stud_no ) {
		return $this->db->getAll("
			SELECT o.*, ma.admin_id
			FROM
			organization o,
			(SELECT m.org_id, a.admin_id FROM 
			(SELECT org_id FROM members WHERE stud_no = ?) m LEFT JOIN
			(SELECT org_id, admin_id FROM admin WHERE stud_no = ?) a 
			ON m.org_id = a.org_id ) ma
			WHERE o.org_id = ma.org_id
			ORDER BY 
			ma.admin_id DESC,
				o.description
			",
			array( $stud_no, $stud_no )
		);
	}

	
	function insertOrg( $name, $desc, $long_desc, $info, $css, $logo )
	{
		$iorg = $this->db->execute("INSERT INTO organization( 
				name,
				description,
				long_description,
				info,
				css,
				logo
			)
			VALUES 
			( ?, ?, ?, ?, ?, ? )", 
			array( $name, $desc, $long_desc, $info, $css, $logo )
		);
		return $this->db->lastInsertId();
	}

	function updateOrg( $name, $desc, $longdesc, $info, $css, $logo, $org_id ) {
		return $this->db->execute("UPDATE organization
			SET name = ?,
				description = ?,
				long_description = ?,
				info = ?
				css = ?,
				logo = ?,
			WHERE org_id = ?",
			array( $name, $desc, $longdesc, $info, $css, $logo, (int) $org_id )
		);
	}

	function updateOrgInfo( $org_id, $info ) {
		$this->db->execute("
			UPDATE organization
			SET info = ?
			WHERE org_id = ?",
			array(
				$info, 
				(int) $org_id
			)
		);
	}
		
	function updateLongDescription( $org_id, $desc ) {
		return $this->db->execute("
			UPDATE organization
			SET long_description = ?
			WHERE org_id = ?",
			array( $desc, (int) $org_id )
		);		
	}
	
	function updateDesc( $org_id, $desc ) {
		return $this->db->execute("
			UPDATE organization
			SET description = ?
			WHERE org_id = ?",
			array( $desc, (int) $org_id )
		);
	}
	
	function updateCss( $org_id, $css ) {
		return $this->db->execute("
			UPDATE organization
			SET css = ?
			WHERE org_id = ?",
			array( $css, (int) $org_id )
		);
	}
	
	function deleteOrg ( $org_id )
	{
		$dorg = $this->db->execute("DELETE FROM organization
			WHERE org_id = ? ",
			array( (int) $org_id )
		);
	}

	function deleteOrgs ( array $orgIds ) {
		$numOrgs = count( $orgIds );
		for( $i=0; $i<$numOrgs; $i++ ) {
			$orgIds[$i] = array( $orgIds[$i] );
		}
		$this->db->executeMultiple(
			"DELETE FROM organization
			WHERE org_id = ?",
			$orgIds);
			
	}
		
}